<template>
  <div class="card content-box">
    <div class="table-box">
      <ProTable ref="proTable" title="字典列表" :columns="columns" :request-api="getTableList">
        <!-- 表格 header 按钮 -->
        <template #tableHeader>
          <el-button v-auth="'dict:dict:save'" type="primary" :icon="CirclePlus" @click="onSave">新增字典</el-button>
        </template>
        <!-- 操作 -->
        <template #operation="scope">
          <el-button v-auth="'dict:dict:update'" type="primary" link :icon="EditPen" @click="onUpdate(scope.row)">
            编辑
          </el-button>
          <el-button type="primary" link :icon="EditPen" @click="onDictItem(scope.row)">字典项</el-button>
          <el-button v-auth="'dict:dict:delete'" type="danger" link :icon="Delete" @click="onDelete(scope.row)"> 删除</el-button>
        </template>
      </ProTable>
      <DictOperateDrawer ref="drawerRef"></DictOperateDrawer>
    </div>
  </div>
</template>

<script setup lang="ts" name="dictManage">
import ProTable from "@/components/ProTable/index.vue";
import { ColumnProps, ProTableInstance } from "@/components/ProTable/interface";
import { addDict, deleteDict, DictInfoResult, getDictPage, getEnumDictItem, updateDict } from "@/api/modules/sys/dict";
import { ref } from "vue";
import { CirclePlus, Delete, EditPen } from "@element-plus/icons-vue";
import { useHandleData } from "@/hooks/useHandleData";
import DictOperateDrawer from "@/views/system/dictManage/DictOperateDrawer.vue";
import { useRouter } from "vue-router";

const router = useRouter();

const columns: ColumnProps[] = [
  { prop: "dictName", label: "字典名称", search: { el: "input", tooltip: "字典名称" } },
  { prop: "dictType", label: "字典类型", search: { el: "input", tooltip: "字典类型" } },
  {
    prop: "dictStatus",
    label: "字典状态",
    enum: () => getEnumDictItem("DictStatus")
  },
  { prop: "remark", label: "字典描述" },
  { prop: "updateTime", label: "更新时间" },
  { prop: "operation", label: "操作", fixed: "right" }
];

const proTable = ref<ProTableInstance>();

const getTableList = param => {
  return getDictPage(param);
};

const drawerRef = ref<InstanceType<typeof DictOperateDrawer> | null>(null);

const onSave = () => {
  drawerRef.value?.openDrawer({
    title: "新增",
    isSave: true,
    isUpdate: false,
    row: {},
    api: addDict,
    getTableList: proTable.value?.getTableList,
    enumMap: proTable.value?.enumMap
  });
};

const onUpdate = (row: Partial<DictInfoResult> = {}) => {
  drawerRef.value?.openDrawer({
    title: "更新",
    isSave: false,
    isUpdate: true,
    row: { ...row },
    api: updateDict,
    getTableList: proTable.value?.getTableList,
    enumMap: proTable.value?.enumMap
  });
};

// 删除信息
const onDelete = async (row: DictInfoResult) => {
  await useHandleData(deleteDict, [row.id], `删除【${row.dictName}】`);
  proTable.value?.getTableList();
};

// 打开字典项的详情页
const onDictItem = (row: DictInfoResult) => {
  router.push({ name: "dictItem", params: { id: row.id }, query: { dictName: row.dictName } });
};
</script>
